package org.manage.service.impl;

import org.manage.dao.EmployeeDao;
import org.manage.dto.PageBean;
import org.manage.entity.Employee;
import org.manage.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户服务层
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeDao employeeDao;

    @Override
    public boolean getUsernameCount(String username) {
        int count = employeeDao.countUsername(username);
        if (count == 0) {
            return false;
        }
        return true;
    }

    @Override
    public void saveEmployee(Employee employee) {
        employeeDao.save(employee);
    }

    @Override
    public int getByUsernameAndPassword(String username, String password) {
    	return employeeDao.findByUsernameAndPassword(username, password);
    }

    @Override
    public List<Employee> getEmployeelist() {
        return employeeDao.list();
    }

    @Override
    public PageBean<Employee> getByPage(int currPage, int pageSize) {
        PageBean<Employee> pageBean = new PageBean<Employee>();
        pageBean.setCurrPage(currPage);
        pageBean.setPageSize(pageSize);
        int totalCount = employeeDao.findCount();
        pageBean.setTotalCount(totalCount);
        double tc = totalCount;
        double num = Math.ceil(tc / pageSize);
        pageBean.setTotalPage((int)num);
        int begin = (currPage - 1) * pageSize;
        List<Employee> lists = employeeDao.findByPage(begin, pageSize);
        pageBean.setList(lists);
        return pageBean;
    }

    @Override
    public Employee getEmployeeById(int eid) {
        return employeeDao.findById(eid);
    }

    @Override
    public void updateEmployee(Employee employee) {
        employeeDao.update(employee);
    }

    @Override
    public void deleteEmployee(int eid) {
        employeeDao.delete(eid);
    }

}
